'''下载m3u8的Flask应用.

只有一个postMethod,名称是'm3u8downPost',关键是导入的线程模块.
post上去的data: {'m3u8':下载的m3u8链接, 'target':下载的目标文件完整路径},
post的返回是{'msg':上传命令是否成功的相关字符串}
'''

import sys
import os

from BaseApp import BaseApp
## 导入线程文件夹:
sys.path.append(os.path.join(sys.path[0], r'threads'))
from m3u8Thread import m3u8Thread

class m3u8FlaskDown(BaseApp):

  def __init__(self):
    BaseApp.__init__(self, 'm3u8FlaskDown')
    self._threadDict['m3u8Thread'] = m3u8Thread()
    return
    
  def postMethod(self, methodName, flaskRequest):
    print('m3u8FlaskDown.postMethod - {methodName}')
    if (methodName == 'm3u8downPost'):
      m3u8 = flaskRequest.form['m3u8']
      target = flaskRequest.form['target']
      flag = self._threadDict['m3u8Thread'].start({
        'm3u8': m3u8, 'target': target
      });
      if flag:
        return {'msg': f'{target}下载文件线程开始'}
      else:
        return {'msg': f'{target}下载线程无法启动,可能正在下载其他文件'}
    return
    